package net.lainiao;

import com.google.common.collect.Lists;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.atomic.AtomicValue;
import org.apache.curator.framework.recipes.atomic.DistributedAtomicLong;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.curator.retry.RetryNTimes;
import org.apache.curator.test.TestingServer;

import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * Created by Administrator on 2016/11/9.
 */
public class DistributedAtomicLongExample {
    private static final int QTY=5;
    private static final String PATH="/DistributedAtomicLongExample/count";

    public static void main(String[] args) throws Exception {
        try(TestingServer server=new TestingServer()){
            CuratorFramework client= CuratorFrameworkFactory.newClient(server.getConnectString(),new ExponentialBackoffRetry(1000,3));
            client.start();
            List<DistributedAtomicLong> examples= Lists.newArrayList();
            ExecutorService service= Executors.newFixedThreadPool(QTY);
            for (int i = 0; i <QTY ; i++) {
                final DistributedAtomicLong count=new DistributedAtomicLong(client,PATH,new RetryNTimes(10,10));
                examples.add(count);
                Callable<Void> task=new Callable<Void>() {
                    @Override
                    public Void call() throws Exception {
                        try {
                            AtomicValue<Long> value=count.add(3L);
                            System.out.println("succeed:"+value.succeeded());
                            if(value.succeeded()){
                                System.out.println("Increment:from "+value.preValue()+"to"+value.postValue());
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        return null;
                    }
                };
                service.submit(task);
            }
            service.shutdown();
            service.awaitTermination(10, TimeUnit.MINUTES);
        }
    }
}
